package subraSoft.centaurus.gui.option;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.TransferHandler;

import org.netbeans.validation.api.ui.ValidationPanel;

import subraSoft.centaurus.gui.components.CenAlert;
import subraSoft.centaurus.gui.option.amb.CenTableModel;
import subraSoft.centaurus.gui.util.CenImageUtil;
import subraSoft.centaurus.model.cobj.CenDefaultCObj;
import subraSoft.centaurus.model.report.CenExcel;

public abstract class CenReportOption extends CenOption implements ActionListener
{
	protected JTable table;
	private CenTableModel model;

	private JButton export2pdf, export2excel, export2clip;
	private JCheckBox onlySelected;

	public CenReportOption()
	{
		ValidationPanel task1 = new ValidationPanel();
		JPanel panel1 = new JPanel( new GridBagLayout() );
		task1.setInnerComponent(panel1);

		// CREACION DE COMPONENTES
		model = getModel();
		table = new JTable(model);
		table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

		CenImageUtil i = CenImageUtil.getInstance();
		export2pdf = new JButton("PDF", i.getIcon("img/doc_pdf.png"));
		export2excel = new JButton("Excel", i.getIcon("img/doc_excel.png"));
		export2excel.addActionListener(this);
		export2clip = new JButton("Portapapeles", i.getIcon("img/clipboard.png"));
		export2clip.addActionListener(this);
		
		onlySelected = new JCheckBox("Exportar solo seleccionadas");

		Container botonera = new Container();
		botonera.setLayout( new BoxLayout( botonera, BoxLayout.X_AXIS));
		botonera.add(new JLabel("Exportar a: "));
		botonera.add(export2pdf);
		botonera.add(export2excel);
		botonera.add(export2clip);

		// VALIDACION

		// LAYOUT
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(5,5,5,5);
		c.anchor = GridBagConstraints.WEST;

		// UBICACION Y LABELS
		c.gridx = 0;
		c.gridy = 0;
		panel1.add(botonera, c);

		c.fill = GridBagConstraints.BOTH;
		c.weightx = 100;
		c.weighty = 100;
		c.gridy = 1;

		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setMaximumSize(new Dimension(600, 150));
		scrollPane.setMinimumSize(new Dimension(600, 150));
		scrollPane.setPreferredSize(new Dimension(600, 150));
		panel1.add(scrollPane, c);

		c.weightx = 0;
		c.weighty = 0;
		c.gridy = 2;
		panel1.add(onlySelected, c);

		addTask(task1);
	}

	@Override
	public void execute() {
		// NO HAY ACCION FINAL
	}

	@Override
	public void initOption(List<CenDefaultCObj> data) 
	{
		model.setDataProvider(data);

		// VISIBLILIDAD
		export2excel.setVisible( (getExcelFileName() != null) );
		export2pdf.setVisible( (getReportFileName() != null) );
	}

	@Override
	public String validateOption() {
		// NO HAY ACCION FINAL, NADA QUE VALIDAR
		return null;
	}

	@Override
	public String validateTask(int index) {
		// NO HAY NADA QUE VALIDAR
		return null;
	}

	public String getReportFileName() {
		return null;
	}

	public abstract CenTableModel getModel();

	@Override
	public void actionPerformed(ActionEvent e) 
	{	
		if( !onlySelected.isSelected())
		{
			table.selectAll();
		}
		
		if(e.getSource() == export2excel)
		{
			try {
				File f = CenExcel.generarExcel(model, table.getSelectedRows(), getExcelFileName());
				new CenReportDialog(f, "img/doc_excel.png");
			} catch (Exception e1) {
				CenAlert.showError("No se pudo generar el reporte", e1);
			}
		}
		else if( e.getSource() == export2clip )
		{
			TransferHandler th = table.getTransferHandler();
			if (th != null) {
				Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
				th.exportToClipboard(table, cb, TransferHandler.COPY);
				CenAlert.showInfo("Informacion copiada al portapapeles");
			}
			
		}
	}

	public String getExcelFileName() {
		return null;
	}
}
